package cn.edu.ctbu.kcsj.service.impl;

import cn.edu.ctbu.kcsj.constain.REnum;
import cn.edu.ctbu.kcsj.domain.Admin;
import cn.edu.ctbu.kcsj.exception.RException;
import cn.edu.ctbu.kcsj.repository.AdminRepository;
import cn.edu.ctbu.kcsj.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AdminImplService implements AdminService {
    @Autowired
    private AdminRepository adminRepository;

    @Override
    public Admin validateAnoAndPassword(Integer ano, String password) throws Exception{
        List<Admin> Admins=adminRepository.findByAno(ano);
        if (Admins.size()>0){
            Admin admin=Admins.get(0);
            if (admin.getPassword().equals(password)){
                return admin;
            }
            else {
                throw new RException(REnum.LOGIN_ERR);}

        }else {
            throw new RException(REnum.LOGIN_ERR);
        }
    }
    public List<Admin> findByAnoAndPassword(Integer ano, String password) {
        return adminRepository.findByAnoAndPassword(ano, password);
    }
    public Admin getByAno(Integer ano){
        Admin admin =adminRepository.findById(ano).orElse(null);
        return admin;
    }
    public Admin update(Admin admin){
        return adminRepository.save(admin);
    }
}
